package com.example.spiretest.controller;


import com.example.spiretest.dto.TestExcel;
import com.example.spiretest.utils.EpiUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: LiuYan
 * @date: 2022/3/21 13:46
 * @description: 生成excel, excel转pdf, pdf转png(不确定是否可以转其他格式图片)
 */
@RestController
@RequestMapping("/ali2/")
public class AsposeController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 生成excel, excel转pdf, pdf转png(不确定是否可以转其他格式图片)
     *
     * @throws IOException
     */
    @RequestMapping(value = "aspose", method = RequestMethod.GET)
    public void summaryDownload2() throws IOException {
        // 假造数据
        List<TestExcel> resultList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            TestExcel testExcel = new TestExcel();
            testExcel.setBillAmount("-10" + i);
            testExcel.setBillCurrency("CNY");
            resultList.add(testExcel);
        }
        // 生成
        HandelEpi(resultList);
    }


    public void HandelEpi(Iterable<?> resultList) {
        try {
            // 生成excel - 创建文件前-先删除
            byte[] excelBytes = EpiUtil.createExcel(resultList);

            // 生成pdf -> 转png
            byte[] imgBytes = EpiUtil.excelToPng(excelBytes);

            System.out.println("excelBytes---" + excelBytes.length + "---imgBytes---" + imgBytes.length);
        } catch (Exception exception) {
            exception.printStackTrace();
        } finally {
            // 最后删除文件夹极其文件
            EpiUtil.operatingPlatformDelete(EpiUtil.EPI_PATH);
        }
    }


}




